﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using EnterpriseLogging=Microsoft.Practices.EnterpriseLibrary.Logging;
using EnterpriseLogFormats=Microsoft.Practices.EnterpriseLibrary.Logging.Formatters;
using EnterpriseLogTraceListeners=Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners;
using Microsoft.Practices.EnterpriseLibrary.Logging;

namespace PermitsAndVisasLeadsSyncher.Helpers.Logging
{
    class LeadsSyncherLogger:ILogger
    {
        static LogWriter writer = null;
        static StringBuilder  log=null;
        private LeadsSyncherLogger()
        {
            if(writer ==null)
            {
                writer = new LogWriter(loggingConfiguration.BuildProgrammaticConfig());
                Logger.SetLogWriter(writer );
            }
            if (log == null)
            {
                log = new StringBuilder();
                log.AppendLine("The details of the leads synched are below !");
            }
        }
        
        //Factory Method
        public static ILogger CreateLogger()
        {
            return new LeadsSyncherLogger ();
        }

        public void LogInfo(string info)
        {
            // append the log to log collection
            log.AppendLine(info);                   
        }

        public void LogError(Exception exception, string extraDetail)
        {
            log.AppendLine("");
            log.AppendFormat("Exception :{0}", exception.Message);            
            if (!string.IsNullOrEmpty(extraDetail))
            {
                log.AppendLine("ExtraDetail : " + extraDetail);
            }
        }

        public void Reset()
        {
            Logger.Write(log.ToString());
            log.Clear();
            // write code to clear the in memory cache of logs
            Logger.Reset();
        }

    }
}
